#!/bin/bash

# 颜色输出定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 彩色输出函数
echo_info() {
    local message="$1"
    local level="$2"
    local color=""
    
    # 根据级别设置颜色
    case "$level" in
        "ERROR"|"error") color="$RED" ;;
        "SUCCESS"|"success") color="$GREEN" ;;
        "WARNING"|"warning") color="$YELLOW" ;;
        *) color="$NC" ;; # 默认无颜色
    esac
    
    echo -e "${color}${message}${NC}"
}

if /etc/init.d/bt status 2>&1 | grep -q "not running"; then
    echo_info "宝塔面板启动中..." WARNING
    /etc/init.d/bt start
else
    echo_info "宝塔面板正在运行" SUCCESS
fi

if /etc/init.d/nginx status 2>&1 | grep -q "nginx is stopped"; then
    echo_info "nginx启动中..." WARNING
    /etc/init.d/nginx start
else
    echo_info "nginx正在运行" SUCCESS
fi

if /etc/init.d/php-fpm-80 status 2>&1 | grep -q "php-fpm dead"; then
    echo_info "php-fpm-80启动中..." WARNING
    /etc/init.d/php-fpm-80 start
else
    echo_info "php-fpm-80正在运行" SUCCESS
fi

if /etc/init.d/redis status 2>&1 | grep -q "Redis is not running"; then
    echo_info "redis启动中..." WARNING
    /etc/init.d/redis start
else
    echo_info "redis正在运行" SUCCESS
fi

if /etc/init.d/mysqld status 2>&1 | grep -q "is not running"; then
    echo_info "mysqld启动中..." WARNING
    /etc/init.d/mysqld start
else
    echo_info "mysqld正在运行" SUCCESS
fi

# 检测并修复容器内的 systemctl 问题
sh fix_systemctl.sh

if systemctl status supervisord 2>&1 | grep -q "Active: inactive (dead)"; then
    echo_info "Supervisord服务启动中..." WARNING
    systemctl start supervisord
else
    echo_info "Supervisord服务正在运行" SUCCESS
fi
